package com.selune.online.ordering.system.project.customer.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.selune.online.ordering.system.project.api.base.entity.GoodsCategory;
import com.selune.online.ordering.system.project.api.base.entity.GoodsInfo;
import com.selune.online.ordering.system.project.api.base.response.Response;
import com.selune.online.ordering.system.project.api.mapper.GoodsCategoryMapper;
import com.selune.online.ordering.system.project.api.mapper.GoodsInfoMapper;
import com.selune.online.ordering.system.project.api.pojo.goods.dto.GoodsSearchDTO;
import com.selune.online.ordering.system.project.api.pojo.goods.vo.CategoryGoodsVO;
import com.selune.online.ordering.system.project.api.pojo.goods.vo.GoodsDetailVO;
import com.selune.online.ordering.system.project.api.pojo.goods.vo.GoodsVO;
import com.selune.online.ordering.system.project.api.service.customer.ICustomerGoodsService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @author xiaoyp
 * @date 2020/3/26
 * <p>
 * Description:
 */
@Service
@Slf4j
public class CustomerGoodsServiceImpl implements ICustomerGoodsService {
	
	@Resource
	private GoodsInfoMapper goodsInfoMapper;
	@Resource
	private GoodsCategoryMapper goodsCategoryMapper;
	
	@Override
	public Response<List<GoodsDetailVO>> listGoods(GoodsSearchDTO goodsSearchDTO) {
		List<GoodsInfo> goodsInfoList = goodsInfoMapper.listGoods(goodsSearchDTO);
		List<GoodsDetailVO> goodsDetailVOList = new ArrayList<>(goodsInfoList.size());
		goodsInfoList.forEach(goodsInfo -> {
			GoodsDetailVO goodsDetailVO = new GoodsDetailVO();
			BeanUtils.copyProperties(goodsInfo, goodsDetailVO);
			goodsDetailVOList.add(goodsDetailVO);
		});
		
		return Response.success(goodsDetailVOList);
	}
	
	@Override
	public Response<List<CategoryGoodsVO>> listCategoryGoods() {
		List<GoodsInfo> goodsInfos = goodsInfoMapper.selectList(null);
		List<GoodsCategory> goodsCategories = goodsCategoryMapper.selectList(null);
		List<CategoryGoodsVO> categoryGoodsVoS = new ArrayList<>(goodsCategories.size());
		
		goodsCategories.forEach(goodsCategory -> {
			CategoryGoodsVO categoryGoodsVO = new CategoryGoodsVO();
			categoryGoodsVO.setName(goodsCategory.getCategoryName());
			categoryGoodsVO.setType(goodsCategory.getCategoryId());
			List<GoodsVO> goodsVoS = new ArrayList<>();
			goodsInfos.forEach(goodsInfo -> {
				if (goodsInfo.getCategoryType().equals(goodsCategory.getCategoryId())) {
					GoodsVO goodsVO = new GoodsVO();
					goodsVO.setId(goodsInfo.getId());
					goodsVO.setName(goodsInfo.getGoodsName());
					goodsVO.setPrice(goodsInfo.getGoodsPrice());
					goodsVO.setDescription(goodsInfo.getGoodsDescription());
					goodsVO.setIcon(goodsInfo.getGoodsPicture());
					goodsVO.setImage(goodsInfo.getGoodsPicture());
					goodsVoS.add(goodsVO);
				}
			});
			if (!CollectionUtils.isEmpty(goodsVoS)) {
				categoryGoodsVO.setFoods(goodsVoS);
				categoryGoodsVoS.add(categoryGoodsVO);
			}
		});
		
		return Response.success(categoryGoodsVoS);
	}
}
